-- agore tem alteracoes ... testing svn only
--
-- more changes herer... lol
--
-- events.lua -> control events in all items and cal the correspondent procedure

-- initial value of the control variable
theScreen = "mainScreen";

function generalControler(e, id, arg)
-- if arg not have value, is a menu item
	if(arg ~= nil) then
		gui.alert(e..' - '..id..'-'..arg)
	else
		gui.alert(e..' - '..id)
	end

	-- main menu options, avaliable in all screens
	if(e==21 and id==1) then
		mmgui:mainScreen()
	elseif(e==21 and id ==2) then
		mmgui:newRegister()
	elseif(e==21 and id==3) then
		mmgui:showRegisterTypes()
	elseif(e==21 and id==4) then
		mmgui:showGraphics()
	elseif(e==21 and id==5) then
		mmgui:syncDevice()
	elseif(e==21 and id==6) then
		mmgui:aboutProgram()
	end


	-- tests by screen name (internal name)
	if(theScreen == "mainScreen") then
		if(e==14 and id==1802 and arg ==1) then
			--gui.selectdate("Select a date")
		elseif(e==14 and id==1802 and arg ==2) then
			--gui.selectdate("Select a date")
		elseif(e==14 and id==1802 and arg ==3) then
			--gui.selectdate("Select a date")
		elseif(e==14 and id==1802 and arg ==4) then
			--gui.selectdate("Select a date")
		elseif(e==14 and id==1802 and arg ==5) then
			gui.selectdate("Initial Date")
			gui.selectdate("Final Date")
		elseif(e==14 and id==1802 and arg ==6) then
			gui.selectdate("Select a Date")
		end
	
		if(e==9 and id==1804 and arg ==0) then
			mmgui:newRegister()
		end

	elseif(theScreen == "aboutProgram") then
		if(e==9 and id==1801 and arg ==0) then
			mmgui:mainScreen()
		end
	
	elseif(theScreen == "showRegisterTypes") then
		if(e==9 and id==1801 and arg ==0) then
			mmgui:newRegisterType()
		end
	
	elseif(theScreen == "newRegisterType") then

	elseif(theScreen == "newRegister") then
		if(e==9 and id ==1809 and arg ==0) then
			ano,mes,dia = gui.selectdate("Select a Date")
			-- 1808 -> the 1809 (button) before component on screen
			-- direct atributions (like this) is dangerous... but...
			gui.settext(1808,ano.."-"..mes.."-"..dia)
		end

	elseif(theScreen == "showGraphics") then

	elseif(theScreen == "syncDevice") then

	end
	

end


-- util.luas -> is a class with various functions
util = {}

function util:existfile(filename)
	return true
end

function util:createcsvfile(filename)
	return true
end


function util:readdata(filename)

	-- texto eh a entrada em util:parsecsv()
	return texto; 
end

function util:writerelpace(oldertext,newtext,filename)

	return true
end

function util:writeall(newtext,filename)
	return true
end

function util:parsecsv(texto)

	return false--array com o arquivo
end

function util:irsend(filename)

	return true
end
-- gui.lua -> draw and manager the user interface

mmgui = {}

-- the menu are displayed at all screens
function mmgui:programMenu()
	menus = {"Registers",
		 "New register",
	         "Register Types",
		 "Graphics",
		 "Synchronize",
		 "About SDmm"}
	gui.menu(menus)
	gui.sethandler(menuSelect,generalControler)
end

function mmgui:aboutProgram()
	theScreen = "aboutProgram";
	gui.destroy()
	screen.clear()
	gui.title("About SDmm")
	mmgui:programMenu()

	text = "This program is a free sofware!\n"
	text = text.."Developer: Helton Eduardo Ritter\n"
	text = text.."Mail: heltonritter@gmail.com\n\n"
	text = text.."Blog: lua.sourcedreams.com.br\n"
	text = text.."Site: www.sourcedreams.com.br\n\n"
	text = text.."Thanks for use Source Dreams Money Manager!"

	
	gui.control{type="field", lines=11, columns=30, text=text}
	gui.nl()
	gui.tab()
	gui.control{type="button", text="Ok", handler=generalControler}

end



function mmgui:showRegisterTypes()
	gui.destroy()
	screen.clear()
	theScreen = "showRegisterTypes";
	gui.title("Your Register types")
	mmgui:programMenu()

	registers ={}
	registers[0]="O valor 1"
	registers[1]="O valor 2"
	registers[2]="O valor 3"
	registers[3]="O valor 4"

	gui.control{type="list", lines=11, columns=31, list=registers, handler=generalControler}
	gui.nl()
	gui.control{type="button", text="New type", handler=generalControler}
	

end


function mmgui:newRegisterType()
	gui.destroy()
	screen.clear()
	theScreen = "newRegisterType";
	gui.title("New Register type")
	mmgui:programMenu()

	gui.label("Register type:")
	gui.nl()
	gui.field(1,30,20)
	gui.nl()

	ch = gui.checkbox("Actived - appear on new register")
	gui.nl()
	
	gui.label("Default value:")
	gui.field(1,10,10)
	gui.nl()
	
	gui.nl()
	gui.control{type="button", text="Save type", handler=generalControler}
	
end

function mmgui:newRegister()	
	theScreen = "newRegister";
	gui.destroy()
	screen.clear()	
	gui.title("New Register")
	mmgui:programMenu()

	gui.label("Value:")
	gui.field(1,10,10)
	gui.nl()

	gui.label("Description:")
	gui.nl()
	gui.field(4,30,100)
	gui.nl()

-- PROBLEM HERE ----------------	
	gui.label("Type:")
	-- change HERE! must read this options for data file
        types = {"food",
	         "Whater",
		 "phone"}

	gui.control{type="popup", list=types, selected=1,handler=generalControler}
	gui.nl()
-- END of PROBLEM HERE ----------------


	gui.label("Date:")
	-- value sugestion: current date
	gui.field(1,10,10,os.date("%Y-%m-%d"))
	gui.control{type="button",text="calendar",handler=generalControler}
	
	gui.nl()

	gui.control{type="button",text="Save", handler=generalControler}
end


function mmgui:showGraphics()
	gui.destroy()
	screen.clear()
	theScreen = "showGraphics";
	gui.title("Graphics results")
	mmgui:programMenu()
	
	dta = {{"teste","value1"},{"nada","value2"},{"outro","mais"}}
	--dta[1] = {}
	--dta[0] ={}
	--dta[2] = {}

--	dta[0] = "Desc1"
	--dta[0][1] = "value1"
--	dta[1][0] = "Desc2"
--	dta[1]1] = "value2"
--	dta[2][1] = "Desc3"
--	dta[2][1] = "Value3"

	gui.list(20,30,dta)
end


function mmgui:syncDevice()
	theScreen = "syncDevice";
	gui.destroy()
	screen.clear()
	gui.title("Synchronize")
	mmgui:programMenu()
	gui.nl()
	gui.label("All data are be exported")
	gui.nl()
	gui.label("and send to other IRdata device.")
	gui.nl()
	gui.control{type="button", text="Send data >", handler=generalControler}
end





-- filter data to display in list on main screen

function mmgui:filterSelected()
	
end



function mmgui:mainScreen()
	gui.destroy()
	screen.clear()
	gui.title("SDmm")
	theScreen = "mainScreen";

	mmgui:programMenu()
	
	gui.label("Filter date on list: ")
	filtros = {"All",
		  "Current Week",
		  "Last Week",
		  "Last month",
		  "Date range",
		  "One date"}
  	gui.control{type="popup", list=filtros, selected=2,handler=generalControler}
	gui.nl()
	
	registers ={}
	registers[0]="O valor 1"
	registers[1]="O valor 2"
	registers[2]="O valor 3"
	registers[3]="O valor 4"
	registers[4]="O valor 4"
	registers[5]="O valor 4"
	registers[6]="O valor 4"
	registers[7]="O valor 4"
	registers[8]="O valor 4"
	registers[9]="O valor 4"

	gui.control{type="list", lines=10, columns=31, list=registers, handler=generalControler}
	gui.nl()
	gui.control{type="button", text="New register", handler=generalControler}
	gui.label("My money:")
	gui.label("R$ 00,00")
	
end

-- mm.lua -> is a logical roules group of functions
mm = {}
function mm:getMyMoney()
	return 50
end


function mm:getMyNoneyat(data)

end

function mm:main()
	mmgui:mainScreen()
	
end
-- main.lua -> this file call the start of the program and call the others specific 
-- functions

mm:main()
gui.main()
